package com.sofwin.controller;

import com.github.pagehelper.PageInfo;
import com.sofwin.Constants;
import com.sofwin.entity.AjaxResult;
import com.sofwin.pojo.SysRole;
import com.sofwin.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @packageName: com.sofwin.controller
 * @author: andyliu
 * @date: 2022/5/4 15:02
 * @email 115176513@qq.com
 * @description: TODO
 */
@Api(tags = "角色管理")
@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @GetMapping
    @ApiIgnore
    public String toData(){
        return "role/data";
    }
    @GetMapping("/toEdit")
    @ApiIgnore
    public String toEidt(Integer id, ModelMap map){
        if(id!=null){
            SysRole role = roleService.queryById(id);
            map.put("role",role);
        }
        return "role/edit";
    }
    @PostMapping("/data")
    @ResponseBody
    @ApiOperation(value = "角色分页查询")
    public AjaxResult data(SysRole role, @RequestParam(required = true,defaultValue = "1") Integer pageNumber,
                           @RequestParam(required = true,defaultValue = Constants.PAGE_SIZE) Integer pageSize){
        PageInfo<SysRole> pageInfo = roleService.queryRoles(role, pageNumber, pageSize);
        return AjaxResult.success(pageInfo);
    }
    @PostMapping("/save")
    @ResponseBody
    @ApiOperation(value="保存角色信息")
    public AjaxResult save(SysRole role,Integer[] resourceIds){
        boolean status = roleService.saveOrUpdateRole(role,resourceIds);
        if(status){
            return AjaxResult.success(status);
        }
        return AjaxResult.error(status);
    }
    @DeleteMapping("/delete/{id}")
    @ResponseBody
    @ApiOperation("根据id删除角色信息")
    public AjaxResult delete(@PathVariable Integer id){
        boolean status = roleService.removeById(id);
        if(status){
            return AjaxResult.success(status);
        }
        return AjaxResult.error(status);
    }
    @DeleteMapping("deletes")
    @ResponseBody
    @ApiOperation("批量删除角色信息")
    public AjaxResult deletes(@RequestParam(name="ids[]") Integer[] ids){
        boolean status = roleService.removeByIds(ids);
        if(status){
            return AjaxResult.success(status);
        }
        return AjaxResult.error(status);
    }
}
